var util = require('../../utils/util.js');

const urlAdd = require('../../utils/config').urlAdd;

//获取应用实例
const app = getApp()

Page({
  data: {
    mobile: '', //商家电话
    mealTimeTitle: '-- 暂无商品 --', //提示
    
    openDate: '', //就餐日期
    openTime: '', //就餐时间
    
    selectVal: '', //选择的日期

    mealTime: [], //餐别
    // mealTimeId: 0, //早中晚餐切换标记
    // mealName: '', //餐别名称
    mealTimeIndex: 0, //餐别标记
    mealTimeId: '',  //餐别ID

    mealIndex: 0,  // 餐品标记
    mealList: '',  //餐种类
    mealId: '', //餐品ID

  },

  toggleType() {
    this.selectComponent('#Calendar').toggleType();
  },

  onLoad: function (options) {
    var that = this;
    that.getMealTime()
  },
  

  
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    var that = this;
    // 判断是否授权
    if (wx.getStorageSync('openId') != '') {
      if (wx.getStorageSync('stuCode') == '' || wx.getStorageSync('stuCode') == null) {
        wx.showToast({
          title: '你已授过权, 请登录',
        })
        wx.redirectTo({
          url: '../../pages/login/login?openId=' + wx.getStorageSync('openId')
        })
      } else {

        that.getMealTime();
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '你还未授权登录，是否授权登录',
        cancelText: '取消',
        cancelColor: 'red',
        confirmText: '确认',
        confirmColor: '#30a1db',
        success: function (res) {
          //点击取消
          if (res.cancel) {

            wx.switchTab({
              url: '../../pages/index/index',
            })
          } else {
            //点击确定
            wx.redirectTo({
              url: '../../pages/userlogin/login'
            })
          }

        },
        fail: function () {}
      })
    }
    
    
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },

  //客服电话
  phoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
      success: function () {
        console.log("成功拨打电话")
      }
    })
  },

  //组件监听事件
  select(e) {
    var that = this;
      that.setData({
        selectVal: e.detail,
      });
    wx.setStorageSync('selectVal', this.data.selectVal);
  },


  //加载餐别
  getMealTime: function () {
    let that = this;
    wx.request({
      url: urlAdd + '/f/wechat/meal/findMealTimeList',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if(res.data.code == 200){
          var list = res.data.data;
          
          console.log('+++++++' + list);
          if (list == null) {
            wx.showToast({
              title:  res.data.message,
              image: '/images/erron.png',
              duration: 2000
            });
          } else {
            var mealTimeId = list[that.data.mealTimeIndex].id
            that.setData({
              mealTime: list,
              mealTimeId: mealTimeId
            })
            //查询餐品
            that.getMeal(mealTimeId);
          }
        } else {
          wx.showToast({
            title: res.data.message,
            image: '/images/erron.png',
            duration: 2000
          });
        }
        
      }
    })
  },

  //早中晚餐选择
  switchCate1: function (event) {
    var that = this;
    // wx.showLoading({
    //   title: '加载中...'
    // });
    var currentTarget = event.currentTarget;
    this.setData({
      mealTimeId: currentTarget.dataset.id,
      mealName: currentTarget.dataset.name
    });
    wx.setStorageSync('mealName', that.data.mealName)
    that.getCatalog(that.data.mealTimeId);
  },


  //下拉选择餐别
  bindPickerChange: function (e) { 
    var that = this;
    var mealTimeIndex = e.detail.value;
    var mealTimeId = that.data.mealTime[mealTimeIndex].id;
    console.log("index===="+ e.detail.value )
    that.setData({
      mealTimeIndex: mealTimeIndex,
      mealTimeId: mealTimeId
    });
    //查询餐品
    that.getMeal(mealTimeId);

  },

  //根据餐别加载餐品
  getMeal: function(mealTimeId){
    let that = this;
    wx.request({
      url: urlAdd + '/f/wechat/meal/findMealByMealTime',
      data: {
        mealTimeId: mealTimeId
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if(res.data.code == 200){
          var list = res.data.data;
          console.log('+++++++' + list);
          if (list == null) {
            wx.showToast({
              title:  res.data.message,
              image: '/images/erron.png',
              duration: 2000
            });
          } else {
            var mealId = list[that.data.mealIndex].id
            that.setData({
              mealList: list,
              mealId: mealId
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            image: '/images/erron.png',
            duration: 2000
          });
        }
        
      }
    })
  },


  //下拉选择餐品
  bindPickerChange2: function (e) { 
    var that = this;
    var mealIndex = e.detail.value;
    console.log("index===="+ e.detail.value )
    that.setData({
      mealIndex: e.detail.value,
      mealId: that.data.mealList[mealIndex].id
    });
  },

  //提交下单
  submitMealOrder: function(){
    let that = this;
    if(that.data.selectVal == ''){
      wx.showToast({
        title: '请选择就餐时间',
        image:'/images/erron.png'
      })
      return;
    }
    if(that.data.mealTimeId == ''){
      wx.showToast({
        title: '请选择餐次',
        image:'/images/erron.png'
      })
      return;
    }
    if(that.data.mealId == ''){
      wx.showToast({
        title: '请选择菜品',
        image:'/images/erron.png'
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确认报餐？',
      success: function(res){
        if(res.confirm){

          wx.showLoading({
            title: '提交中...'
          })
          wx.request({
            url: urlAdd + '/f/wechat/meal/submitMealOrder',
            data: {
              weixinOpenId: wx.getStorageSync('openId'),   //openId
              mealTimeId: that.data.mealTimeId,    //餐别
              mealIds: that.data.mealId,   //餐品
              useDate: that.data.selectVal,   //用餐时间
            },
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              wx.hideLoading();
              if(res.data.code == 200){
                wx.showModal({
                  title: '提示',
                  content: '报餐成功，是否继续报餐？',
                  success: function(res){
                    if(res.confirm){
      
                    } else if(res.cancel){
                      wx.switchTab({
                        url: '/pages/index/index',
                      })
                    }
                  }
                })
              } else if(res.data.code == 1201){
                wx.showModal({
                  title: '提示',
                  content: '不在点餐时间范围内',
                  showCancel: false,
                  success: function(res){

                  }
                })
              } else{
                wx.showToast({
                  title: '报餐失败',
                  image: '/images/erron.png',
                  duration: 2000
                });
              }
              
            },fail: function () {
              wx.hideLoading();
              wx.showToast({
                title: '系统错误',
                image: '/images/erron.png',
                duration: 2000
              });
            }
          })
        } else if(res.cancel){

        }
      }
    })
  },
  baoCanDetail: function(){
    wx.navigateTo({
      url: '/pages/baocanList/baocanList',
    })
  }
})